# File：01 mysql连接.py
# Author：信思智学
# Date ：2025/7/17 14:37
# Info：python+mysql ======>   pymysql
import pymysql

# 创建数据库连接  connection
conn = pymysql.connect(
    user="root",
    password="root",
    host="127.0.0.1",
    port=3306,
    database="spt2505"
)

# 通过数据库连接创建数据库对象
cur = conn.cursor()

username = input("请输入用户名")
password = input("请输入密码")

# 准备sql语句  登录


# 参数替换方式三：
sql = "select * from t_user where username = %s and password = %s"
print(sql)

# 通过数据库对象执行sql  元组类型
# sql = "select * from t_user where username = %s and password = %s"
# count = cur.execute(sql,(username,password))

# 通过数据库对象执行sql  列表类型
# sql = "select * from t_user where username = %s and password = %s"
# count = cur.execute(sql,[username,password])

# 通过数据库对象执行sql  字典类型
sql = "select * from t_user where username = %(name)s and password = %(pass)s"
count = cur.execute(sql,{"name":username,"pass":password})

result = cur.fetchall()

print(result)
if result:
    print("登录成功")
else:
    print("用户名或密码错误！")

# 关闭数据库对象
cur.close()
# 关闭数据库连接
conn.close()